package com.homelock.service.check;

import org.apache.commons.lang3.StringUtils;

import com.pi.base.exception.ServiceException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LockUserCheckUtil {

  /**
   * check锁具用户信息，包括check手机号
   * @param devId
   * @param lockUserName
   */
  public static void checkLockUserAdd(Long devId, String lockUserName,String mobile) {
    validateDevId(devId);
    validateLockUserName(lockUserName);
    validateMobile(mobile);
  }

  public static void checkLockUserAdd(Long devId, String lockUserName) {
    validateDevId(devId);
    validateLockUserName(lockUserName);
  }

  public static void checkLockUserDel(Long devId, Long userId) {
    validateDevId(devId);
    validateUserId(userId);
  }

  public static void checkLockUserQuery(Long devId, Long userId) {
    validateDevId(devId);
    validateUserId(userId);
  }

  public static void checkUserEmergency(String contactName,String contactMobile){
    validateContactName(contactName == null ? null : contactName.trim());
    validateMobile(contactMobile);
  }


  public static void checkLockUserListQuery(Long devId) {
    validateDevId(devId);
  }
  
  public static void checkUserLockListQuery(Long userId) {
    validateUserId(userId);
  }
  
  public static void checkLockUserQueryByAppId(Long devId, Long userId) {
      validateDevId(devId);
      validateUserId(userId);
    }

  public static void checkLockUserNameUpdate(Long devId, Long userId, String lockUserName) {
    validateDevId(devId);
    validateUserId(userId);
    validateLockUserName(lockUserName);
  }

  public static void checkSwitchLockUserAdmin(Long devId, Long userId, Boolean adminFlag) {
    validateDevId(devId);
    validateUserId(userId);
    validateAdminFlag(adminFlag);
  }

  public static void validateMobile(String mobile) {
    if (StringUtils.isBlank(mobile)) {
      throw new ServiceException("手机号为空");
    }
    Pattern pattern = Pattern.compile("^(1)\\d{10}$");
    Matcher matcher = pattern.matcher(mobile);
    if(!matcher.matches()){
      throw new ServiceException("手机号不是11个数字");
    }
  }
  private static void validateDevId(Long devId) {
    if (null == devId || devId <= 0) {
      throw new ServiceException("设备编号为空");
    }
  }

  private static void validateUserId(Long userId) {
    if (null == userId || userId <= 0) {
      throw new ServiceException("用户编号为空");
    }
  }

  private static void validateLockUserName(String lockUserName) {
    if (StringUtils.isBlank(lockUserName)) {
      throw new ServiceException("用户名称为空");
    }
  }

  private static void validateAdminFlag(Boolean adminFlag) {
    if (null == adminFlag) {
      throw new ServiceException("权限开关无效");
    }
  }

  private static void validateContactName(String contactName) {
    if (StringUtils.isBlank(contactName)) {
      throw new ServiceException("紧急联系人名称为空");
    }
  }


}
